package com.dzqc.fly.controller.pc;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.json.JSONUtil;
import com.dzqc.fly.common.Result;
import com.dzqc.fly.db.form.LoginForm;

import com.dzqc.fly.db.form.UpdateClassForm;
import com.dzqc.fly.db.form.UpdateClassofFdyForm;
import com.dzqc.fly.db.form.UpdatePasswordForm;
import com.dzqc.fly.service.UserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import java.util.HashMap;
import java.util.Set;


@RestController
@RequestMapping("/user")
@Tag(name = "UserController", description = "pc端用户Web接口")
@Slf4j
@EnableAutoConfiguration
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/login")
    @Operation(summary = "pc端登陆系统")
    public Result login(@Valid @RequestBody LoginForm form , HttpServletRequest request){
        HashMap param= JSONUtil.parse(form).toBean(HashMap.class);
        System.out.println(param);

        HttpSession session   =   request.getSession();
        session.setAttribute("userSon",form.getSno());
        session.setAttribute("passWord",form.getPassword());
        Integer userId=userService.login(param);
        Result result=Result.ok().put("result",userId!=null?true:false);
        if (userId != null) {
            StpUtil.setLoginId(userId);
            Set<String> roles=userService.searchUserRole(userId);
            String token=StpUtil.getTokenInfo().getTokenValue();
            result.put("roles",roles).put("token",token);
            session.setAttribute("roles",roles);
            System.out.println(roles+"================");
        }
        log.info("登录了");
        return result;
    }

    @PostMapping("/updatePassword")
    @SaCheckLogin
    @Operation(summary = "pc端修改密码")
    public Result updatePassword(@Valid @RequestBody UpdatePasswordForm form){
        int userId=StpUtil.getLoginIdAsInt();
        HashMap param=new HashMap(){{
            put("userId",userId);
            put("password",form.getPassword());
        }};
        int rows=userService.updatePassword(param);
        return Result.ok().put("rows",rows);
    }

    @GetMapping("/logout")
    @Operation(summary = "pc端退出系统")
    public Result logout(){
        StpUtil.logout();
        return Result.ok();
    }

    @PostMapping("/updateclassoffdy")
    @SaCheckLogin
    @Operation(summary = "pc端修改所在班级的辅导员")
    public Result updateclassoffdy(@Valid @RequestBody UpdateClassofFdyForm form){
        HashMap param=new HashMap(){{
            put("fdyid",form.getFdyid());
            put("fdy",form.getFdy());
            put("colid",form.getColid());
        }};
        int rows=userService.updateclassoffdy(param);
        return Result.ok().put("rows",rows);
    }

    @PostMapping("/updateClass")
    @SaCheckLogin
    @Operation(summary = "pc端修改学生所在的专业班级")

    public Result updateClass(@Valid @RequestBody UpdateClassForm form){
        int userId=StpUtil.getLoginIdAsInt();
        HashMap param=new HashMap(){{
            put("userId",userId);
            put("class_id",form.getClass_id());
            put("class_name",form.getClass_name());
        }};
        int rows=userService.updateClass(param);
        return Result.ok().put("rows",rows);
    }
}
